package com.reesechou.controller.blog;

import com.reesechou.javabean.Article;
import com.reesechou.javabean.Comment;
import com.reesechou.javabean.User;
import com.reesechou.service.*;
import org.directwebremoting.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jingqing.zhou on 2015/5/12.
 */
@Controller
@RequestMapping("/jsp/blog")
public class ArticleViewController {
    private static Logger logger = Logger.getLogger(ArticleViewController.class);

    private static final String VIEW_BLOG = "blog_view";

    @Autowired
    private UserService userService ;

    @Autowired
    private TagService tagService ;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService ;

    @Autowired
    private CommentService commentService ;

    @Autowired
    private SiteConfigService siteConfigService ;

    @Autowired
    private LinkService linkService ;
    /**
     * 这个类是用来处理 类型之间的转换的问题，如 date --> String 之间的转换
     * @param binder
     */
    @InitBinder
    private void dateBinder(WebDataBinder binder) {
        //The date format to parse or output your dates
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //Create a new CustomDateEditor
        CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
        //Register it as custom editor for the Date type
        binder.registerCustomEditor(Date.class, editor);
    }

    @RequestMapping(value="/blog_view")
    public String blogView(Model model, Article article){
        article.setView(article.getView()+1);
        articleService.update(article);
        Article article1 = articleService.queryById(article.getId());
        model.addAttribute("article",article1);
        createReferenceData(model,article.getId());
        return VIEW_BLOG;
    }

   private void createReferenceData(Model model,int articleId){
       model.addAttribute("user",userService.find(new User()));
       model.addAttribute("tags",tagService.getAllTag());
       model.addAttribute("category",categoryService.findAllCategory());
       model.addAttribute("siteConfig",siteConfigService.findAllSiteConfig());

       //TODO:还要存放这篇文章下的所有评论
       model.addAttribute("comments", commentService.findByArticleId(articleId));

       //TODO:最新的五篇文章
       model.addAttribute("newestArticle", articleService.findNewestArticle());

       //TODO:最新的五篇评论
       model.addAttribute("newestComment", commentService.findNewestComment());

       //TODO:最新的五个友情链接
       model.addAttribute("newestLink", linkService.findNewestLink());

       //这里是处理页面中的导航条的部分 对应的List 为 navItems , 类型值为 List<Article>
       List<Article> navItems = articleService.findPage();
       model.addAttribute("navItems",navItems);
   }

    @RequestMapping(value="/comment_add",method = RequestMethod.POST)
    public String commentAdd(Model model, Comment comment){
        String postIP = "" ;//TODO;
        Date postTime = new Date();//发表的时间
        comment.setPostIP(postIP);
        comment.setPostTime(postTime);
        commentService.insert(comment);
//        Integer.parseInt("abc");
        createReferenceData(model,comment.getArticleId());
        return "redirect:"+ VIEW_BLOG +"?id="+comment.getArticleId() ;////重定向，防止刷新的时候评论重复提交的问题
    }
}
